use rmpredictdb::model::{ConvertAble, User};
use serde_json::{Map, Value};

use crate::func::ValueExt;

pub trait Take {
    type OutPut: ConvertAble;
    fn take(self) -> Self::OutPut;
}
#[derive(Default, Debug)]
pub struct InnerUser {
    user: User,
}
impl Take for InnerUser {
    fn take(self) -> Self::OutPut {
        let InnerUser { user } = self;
        println!("{:#?}", user);
        user
    }

    type OutPut = User;
}

impl From<&Map<String, Value>> for InnerUser {
    fn from(value: &Map<String, Value>) -> Self {
        let mut inner = InnerUser::default();
        inner
            .user
            .set_username(value.get("username").unwrap().trim_ref());
        inner
            .user
            .set_password(value.get("password").unwrap().trim_ref());
        inner
            .user
            .set_last_predict(Some(value.get("last_predict").unwrap().trim_ref()));
        inner
            .user
            .set_last_result(Some(value.get("last_result").unwrap().trim_ref()));
        inner
            .user
            .set_id(value.get("id").unwrap().as_i64().unwrap() as i32);
        inner
    }
}
